package com.yimi.yinhepda.db.dao;

import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.UCDeptTable;
import com.pda.uce.commom.util.LogUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: lj
 * Date: 2019/9/28 14:19
 * Description:优速网点表操作类
 */
public class UCDeptTableDao extends BaseDao<UCDeptTable, Long> {

    public UCDeptTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), UCDeptTable.class);
    }

    public void createOrUpdate(List<UCDeptTable> list) {
        SQLiteDatabase database = getDbHelper().getWritableDatabase();
        String tableName = dao.getTableName();
        StringBuffer sql = new StringBuffer("replace into ");
        sql.append(tableName);
        sql.append("(baseOrgCode,");
        sql.append("orgId,");
        sql.append("orgName,");
        sql.append("orgType,");
        sql.append("deleteFlag,");
        sql.append("deptCode,");
        sql.append("deptName,");
        sql.append("deptType,");
        sql.append("latestTime)");

        //10
        sql.append("values(?,?,?,?,?,?,?,?,?)");
        LogUtil.e("sql=" + sql.toString());

        database.beginTransaction();
        for (UCDeptTable item : list) {
            Object[] values = new Object[]{
                    item.getBaseOrgCode(), item.getOrgId(), item.getOrgName(), item.getOrgType(),
                    item.getDeleteFlag(), item.getDeptCode(), item.getDeptName(), item.getDeptType(), item.getLatestTime()
            };
            database.execSQL(sql.toString(), values);
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    }

    public UCDeptTable findDataByBaseOrgCode(String nextDeptCode) {
        try {
            QueryBuilder<UCDeptTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("baseOrgCode", nextDeptCode);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public long queryDataAllCount() {
        try {
            QueryBuilder<UCDeptTable, Long> queryBuilder = dao.queryBuilder();
            return queryBuilder.countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 分页查询数据
     */
    public List<UCDeptTable> findDataByPage(String deptNameCode, long currentPage, long pageSize) {
        try {
            QueryBuilder<UCDeptTable, Long> queryBuilder = dao.queryBuilder();
            if (deptNameCode != null && !"".equals(deptNameCode)) {
                Where where = queryBuilder.where();
                where.like("baseOrgCode", "%" + deptNameCode + "%");
                where.or().like("orgName", "%" + deptNameCode + "%");
            }
            return queryBuilder.offset(currentPage * pageSize).limit(pageSize).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据部门编码查询对应快递网点
     */
    public UCDeptTable findDataByDeptCode(String deptCode) {
        try {
            QueryBuilder<UCDeptTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("deptCode", deptCode);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
